package com.itheima.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.SmsHomeAdvertiseMapper;
import com.itheima.pojo.SmsHomeAdvertise;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;

import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@DubboService
public class SmsHomeAdvertiseImpl implements SmsHomeAdvertiseApi {
    @Autowired
    private SmsHomeAdvertiseMapper smsHomeAdvertiseMapper;

    /**
     * 添加广告
     *
     * @param smsHomeAdvertise
     * @return
     */
    @Override
    public Boolean save(SmsHomeAdvertise smsHomeAdvertise) {
        LambdaUpdateWrapper<SmsHomeAdvertise> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(SmsHomeAdvertise::getName, smsHomeAdvertise.getName());
        SmsHomeAdvertise smsHomeAdvertise1 = smsHomeAdvertiseMapper.selectOne(wrapper);
        if (smsHomeAdvertise1 != null) {
            return false;
        }
        //执行添加
        smsHomeAdvertiseMapper.insert(smsHomeAdvertise);

        return true;
    }


    /**
     * 删除
     *
     * @param ids
     */
    @Override
    public void delete(Integer[] ids) {
        List<Integer> integers = Arrays.asList(ids);
        //如果状态为 1 启用 则不允许删除
        smsHomeAdvertiseMapper.deleteBatchIds(integers);

    }


    /**
     * 分页查询广告
     *
     * @param pageNum
     * @param pageSize
     * @param endTime
     * @param type
     * @param name
     * @return
     */
    @Override
    public IPage<SmsHomeAdvertise> list(Integer pageNum, Integer pageSize, String endTime, Integer type, String name) {
        IPage<SmsHomeAdvertise> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<SmsHomeAdvertise> wrapper = new LambdaQueryWrapper<>();

        wrapper.le(!ObjectUtils.isEmpty(endTime), SmsHomeAdvertise::getEndTime, endTime)
                .like(!ObjectUtils.isEmpty(name), SmsHomeAdvertise::getName, name)
                .eq(!ObjectUtils.isEmpty(type), SmsHomeAdvertise::getType, type);
        IPage<SmsHomeAdvertise> pageResult = smsHomeAdvertiseMapper.selectPage(page, wrapper);
        return pageResult;
    }


    /**
     * 根据id 修改上线下线状态
     *
     * @param id
     * @param status
     */
    @Override
    public void update(Integer id, Integer status) {
        SmsHomeAdvertise smsHomeAdvertise = new SmsHomeAdvertise();
        smsHomeAdvertise.setId(id.longValue());
        smsHomeAdvertise.setStatus(status);
        smsHomeAdvertiseMapper.updateById(smsHomeAdvertise);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @param
     */
    @Override
    public SmsHomeAdvertise findByID(Integer id) {
        SmsHomeAdvertise smsHomeAdvertise = smsHomeAdvertiseMapper.selectById(id.longValue());
        return smsHomeAdvertise;
    }


    /**有判断修改
     * @param smsHomeAdvertise
     */
    @Override
    public Boolean updateById(SmsHomeAdvertise smsHomeAdvertise) {
        //走进来 就不会出现自己和自己相同的情况
        //先查询 是否有重复的
        LambdaUpdateWrapper<SmsHomeAdvertise> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(SmsHomeAdvertise::getName,smsHomeAdvertise.getName());
        SmsHomeAdvertise smsHomeAdvertise1 = smsHomeAdvertiseMapper.selectOne(wrapper);
        if(smsHomeAdvertise1!=null){
            //重复
            return false;
        }
        smsHomeAdvertiseMapper.updateById(smsHomeAdvertise);
        return true;
    }

    /**
     * 无判断直接修改
     *
     * @param smsHomeAdvertise
     */
    @Override
    public void updateByIdNo(SmsHomeAdvertise smsHomeAdvertise) {
        smsHomeAdvertiseMapper.updateById(smsHomeAdvertise);
    }
}
